import { _decorator, Component, Node } from 'cc';
import { NodePoolManager } from './NodePoolManager';
import { Battle } from './Battle';
import { Globals } from './Globals';
import { skillType } from './Data/SkillData';
const { ccclass, property } = _decorator;

@ccclass('Knife')
export class Knife extends Component {

    //技能数据
    skillData: skillType = null;
    // 旋转速度
    angleSpeed: number = 200;
    // isMoving: boolean = true;
    // 刀子数量
    KnifeItemNum: number = 1;
    start() {
        this.createKnifeItem(this.KnifeItemNum);
    }

    update(deltaTime: number) {
        // 刀子旋转
        this.angleKnife(deltaTime);
    }

    // 添加刀子item函数
    createKnifeItem(num: number) {
        // 实例化刀子item节点
        for (let i = 0; i < num; i++) {
            // const knifeItemNode = NodePoolManager.instance.get(Globals.prefabs["Knife_Item"]);
            // const knifeItemNode = NodePoolManager.instance.get(Battle.prefabs["Knife_Item"]);
            const knifeItemNode = Globals.getNode('Knife_Item', this.node);

            // const knifeItemNode = Globals.getNode("Knife_Item",this.node);
            // knifeItemNode.parent = this.node;

            // // 刀子偏移角度
            const itemRotation = (360 / num) * i;

            knifeItemNode.angle = itemRotation;
        }
    }

    // 旋转
    angleKnife(deltaTime) {
        // const Knife = NodePoolManager.instance.get(Battle.prefabs["Knife"]);
        this.node.angle += this.angleSpeed * deltaTime;
        this.node.setWorldPosition(Battle.playerNode.getWorldPosition());
    }

    // 更新刀子数据
    updateKnifeData(data: skillType) {
        // 更新数据
        this.skillData = data;
        this.KnifeItemNum = data.level;
        this.angleSpeed = 200 - data.level * 2;
        // 销毁所有刀子item，重新创建
        this.node.destroyAllChildren();
        this.createKnifeItem(this.KnifeItemNum);
    }
}


